import {Node, UITransform, v2, v3, Vec2, Vec3 } from "cc";
import emojiAware from 'emoji-aware';

export class BCTools {
	//判断value是否是纯数字
	static isNumber(value: any): boolean{
		return !isNaN(value);
	}

	//解决小数点精度不准确出现多位小数的问题
	static toDecimal(x: number, length: number = 2): number {
		let val: number = Number(x);
		if (!isNaN(x)) {
			let valStr: string = val.toFixed(length);
			return Number(valStr);
		}
		return val;
	}

	//数字转带有逗号分隔符的数字
	static bigNumber2Str(num: number): string {
		return num.toLocaleString("en-US");
	}

	//大数字缩写
	private static NUMBER_UNIT: string[] = ["", "k", "M", "B", "T", "E"];
	static bigNumber2Short(num: number): string {
		const base = 1000;
		const decimals = 1;
		if (num < base) {
			return num.toString();
		}
		const exponent = Math.floor(Math.log10(num) / Math.log10(base));
		const abbreviatedNumber = (num / Math.pow(base, exponent)).toFixed(decimals);

		return abbreviatedNumber + BCTools.NUMBER_UNIT[exponent];
	}

	//超过长度的字符显示...
	static subStringShow(oldStr: string, length: number): string {
		const chars: string[] = emojiAware.split(oldStr);
		if (chars.length<=length){
			return oldStr;
		}
		const substr: string = chars.slice(0, length).join('');
		return substr+"...";
	}

	static randomInt(startNum: number, endNum: number): number {
		return Math.floor(Math.random() * (endNum - startNum + 1) + startNum);
	}

	/**
	 * 数字前面自动补0
	 */
	static PrefixInteger(num, length) {
		return (Array(length).join('0') + num).slice(-length);
	}

	//时间戳转日期
	static time2Date(time: number): string {
		const date = new Date(time);
		const dateStr = `${BCTools.PrefixInteger(date.getFullYear(), 4)}-${BCTools.PrefixInteger(date.getMonth() + 1, 2)}-${BCTools.PrefixInteger(date.getDate(), 2)}`;
		return dateStr;
	}

	//秒转时分秒
	static secondToTime(second: number): string{
		const hour = Math.floor(second / 3600);
		const min = Math.floor((second%3600)/60);
		const sec = second-hour*3600-min*60;
		return `${hour > 0 ? this.PrefixInteger(hour, 2)+':':''}${this.PrefixInteger(min, 2)}:${this.PrefixInteger(sec, 2)}`;
 	}

	//版本号比较
	static compareVersion(ver1: string, ver2: string): boolean {
		const tempVer1 = ver1.split(".");
		const tempVer2 = ver2.split(".");
		for (let i = 0; i < tempVer1.length; ++i) {
			const v1 = parseInt(tempVer1[i]);
			const v2 = parseInt(tempVer2[i]);
			if (v1 === v2) {
				continue;
			} else {
				return v2 - v1 > 0;
			}
		}

		if (tempVer2.length > tempVer1.length) {
			return true;
		} else {
			return false;
		}
	}

	//深拷贝对象
	static copyJsonObj(obj: Object): any {
		let neweObj = null;
		try {
			const objStr = JSON.stringify(obj);
			neweObj = JSON.parse(objStr);
		} catch (e) {

		}
		return neweObj;
	}


	/**
	 * 判断两个数组中是否包含相同元素
	 */
	static arrayIsEqualSameItem(arr1: any[], arr2: any[]): boolean {
		for (let item1 of arr1) {
			for (let item2 of arr2) {
				if (item1 == item2) {
					return true;
				}
			}
		}
		return false;
	}

	//坐标点转节点坐标
    static convertToNodeSpaceAR(node: Node, point: Vec2|Vec3): Vec2{
        const ui = node.getComponent(UITransform);
        if(!ui){
            return v2(point.x, point.y);
        }
		const resultPos = ui.convertToNodeSpaceAR(v3(point.x, point.y));
        return v2(resultPos.x, resultPos.y);
    }

	//计算两点之间距离
	static calculateDistance(v1: Vec3, v2: Vec3) {
		const deltaX = v2.x - v1.x;
		const deltaY = v2.y - v1.y;
		const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
		return distance;
	  }
}

